﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ModelLayer;
// services
using MovieCtrlService;
using ScheduleCtrlService;
using SeatCtrlService;
using ReservationCtrlService;
using System.Text;
using System.IO;

public partial class Default2 : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (MovieList.Items.Count == 0)
		{
			MovieCtrlClient movieService = new MovieCtrlClient();
			LoadMovies(movieService);
			movieService.Close();
		}

		ScheduleList.Visible = false;
		ScheduleListLabel.Visible = false;
	}

	protected void MovieList_TextChanged(object sender, EventArgs e)
	{
		ScheduleCtrlClient scheduleService = new ScheduleCtrlClient();
		LoadSchedules(scheduleService);
		scheduleService.Close();

		ScheduleList.Visible = true;
		ScheduleListLabel.Visible = true;
	}

	protected void ScheduleList_TextChanged(object sender, EventArgs e)
	{
		ScheduleList.Visible = true;
		ScheduleListLabel.Visible = true;
	}

	private void LoadMovies(MovieCtrlClient movieService)
	{
		Movie[] movies = movieService.getMovies();
		if (movies == null)
			return;

		ListItem item = new ListItem("Select a movie...", "0");
		MovieList.Items.Add(item);
		for (int i = 0; i < movies.Length; ++i)
		{
			item = new ListItem(movies[i].name, movies[i].id.ToString());
			MovieList.Items.Add(item);
		}
	}

	private void LoadSchedules(ScheduleCtrlClient scheduleService)
	{
		ScheduleList.Items.Clear();

		int selected = Int32.Parse(MovieList.SelectedValue);
		Schedule[] schedules = scheduleService.getScheduleByMovieId(selected);
		if (schedules.Length < 1)
			return;

		ListItem item = new ListItem("Select a time...", "0");
		ScheduleList.Items.Add(item);
		foreach (Schedule s in schedules)
		{
			item = new ListItem(s.time.ToString() + " (Room: " + s.room.id, s.id.ToString());
			ScheduleList.Items.Add(item);
		}
	}
}